<template>
  <div class="list">
    <el-form :inline="true" :model="page" class="demo-form-inline">
      <el-form-item label="博客标题">
        <el-input v-model="page.params.blogTitle" clearable filterable placeholder="博客标题" />
      </el-form-item>
      <el-form-item label="分类">
        <!-- 可清空 可搜索 -->
        <el-select v-model="page.params.typeId" clearable filterable placeholder="分类">
          <el-option v-for="type in typeList" :key="type.typedId" :label="type.typeName" :value="type.typeId" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="getByPage">查询</el-button>
      </el-form-item>
    </el-form>

    <!-- 添加按钮 -->
    <el-button type="primary" class="btn-class" @click="openAddDialogVisible()">添加</el-button>

    <!-- 表格 -->
    <el-table :data="page.list" border style="width: 100%" @sort-change="changeSort">
      <el-table-column prop="blogTitle" label="标题" width="200" show-overflow-tooltip />
      <el-table-column prop="typeName" label="分类" sortable="custom" />
      <el-table-column prop="blogImage" label="封面">
        <template slot-scope="scope">
          <el-image
            style="width: 100px; height: 100px"
            :src="scope.row.blogImage"
            :preview-src-list="[scope.row.blogImage]"
          />
        </template>
      </el-table-column>
      <el-table-column prop="blogGoods" label="点赞数" width="50" sortable="custom" />
      <el-table-column prop="blogRead" label="阅读时" width="50" sortable="custom" />
      <el-table-column prop="blogCollection" label="收藏数" width="50" sortable="custom" />
      <el-table-column prop="blogComment" label="评论数" width="50" sortable="custom" />
      <el-table-column prop="blogSource" label="文章来源" />
      <el-table-column prop="createdTime" label="创建时间" sortable="custom" />
      <el-table-column prop="updateTime" label="修改时间" sortable="custom" />
      <el-table-column prop="blogRemark" label="备注" width="200" show-overflow-tooltip />

      <!-- 操作 -->
      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-dropdown>
            <el-button type="primary">
              操作<i class="el-icon-arrow-down el-icon--right" />
            </el-button>
            <el-dropdown-menu slot="dropdown">
              <el-dropdown-item>
                <el-button size="mini" type="primary" @click="handleEdit(scope.row.blogId)">编辑</el-button>
              </el-dropdown-item>
              <el-dropdown-item>
                <el-button size="mini" type="danger" @click="handleDelete(scope.row.blogId)">删除</el-button>
              </el-dropdown-item>
              <el-dropdown-item>
                <el-button size="mini" type="success" @click="toRead(scope.row.blogId)">阅读</el-button>
              </el-dropdown-item>
            </el-dropdown-menu>
          </el-dropdown>
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页 -->
    <el-pagination
      class="page"
      :current-page="page.currentPage"
      :page-sizes="[10,20,50,100]"
      :page-size="page.pageSize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="page.totalCount"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
    />

    <!-- 添加 -->
    <el-dialog title="添加" :visible.sync="dialogVisible">
      <blog-add @closeAddDialogVisible="closeAddDialogVisible" @getByPage="getByPage" />
    </el-dialog>

    <!-- 修改 -->
    <el-dialog title="修改" :visible.sync="updateDialog">
      <blog-update :blog="blog" @closeUpdateDialog="closeUpdateDialog" @getByPage="getByPage" />
    </el-dialog>

    <!-- 阅读 -->
    <el-dialog title="查看" :visible.sync="readDialog">
      <div v-html="blog.blogContent" />
    </el-dialog>
  </div>
</template>
<script>

import blogApi from '@/api/blog'
import blogAdd from './blog-add'
import blogUpdate from './blog-update'
export default {
  name: 'BlogList',
  components: {
    blogAdd,
    blogUpdate
  },
  data() {
    return {
      page: {
        currentPage: 1, // 当前页
        pageSize: 10, // 每页显示条数
        totalPage: 0, // 总页数
        params: {}, // 参训参数对象
        list: [], // 数据
        sortColumn: '', // 排序列
        soreMethod: 'asc' // 排序方式
      },
      blog: {
        blogId: '',
        blogTitle: '',
        blogImage: null,
        blogContent: null,
        typeName: '',
        blogRemark: null
      },
      updateDialog: false,
      dialogVisible: false,
      readDialog: false,
      typeList: this.$store.getters.typeList // 获取分类
    }
  },
  // 钩子函数，实例化之前执行
  created() {
    this.getByPage()
    // 延迟加载
    setTimeout(() => {
      this.typeList = this.$store.getters.typeList
    }, 500)
  },
  methods: {
    // 获取分页信息
    getByPage() {
      blogApi.getByPage(this.page).then(res => {
        console.log(res.data)
        this.page = res.data
      })
    },
    // 排序
    changeSort(e) {
      // console.log(e)
      if (e.order) {
        this.page.sortColumn = e.prop
        this.page.soreMethod = e.order
      } else {
        this.page.sortColumn = ''
        this.page.soreMethod = 'asc'
      }
      // 再次渲染
      this.getByPage()
    },
    // 修改
    handleEdit(id) {
      blogApi.get(id).then(res => {
        this.blog = res.data
        this.updateDialog = true
      })
    },
    // 删除
    handleDelete(id) {
      // 删除
      this.$confirm('是否删除?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        // 调用删除接口
        blogApi.deleteById(id).then(res => {
          this.$message.success(res.msg)
          // 刷新表格
          this.getByPage()
        })
      })
    },
    // 打开添加
    openAddDialogVisible() {
      this.dialogVisible = true
    },
    // 关闭添加
    closeAddDialogVisible() {
      this.dialogVisible = false
    },
    // 分页
    handleSizeChange(val) {
      // console.log(`每页 ${val} 条`)
      this.page.pageSize = val
      this.getByPage()
    },
    handleCurrentChange(val) {
      // console.log(`当前页: ${val}`)
      this.page.currentPage = val
      // 再次渲染
      this.getByPage()
    },
    // 关闭修改
    closeUpdateDialog() {
      this.updateDialog = false
    },
    // 阅读
    toRead(id) {
      blogApi.getRead(id).then(res => {
        this.blog = res.data
        this.readDialog = true
      })
    }

  }
}
</script>
